﻿using IFRS.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace IFRS.DataAccess.DAL
{
    public class SPR_INDIVIDUALIMPAIR
    {
        public DataTable SEARCH_ALL(IndividualImpair individualImpair, Paging paging, bool IFRS_ALLBRANCH, string sIFRS_COST_CENTER, ref int iRecordCount)
        {
            DataTable dt = new DataTable();
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@RecordCount", SqlDbType.Int));
                lstParams[0].Direction = ParameterDirection.Output;
                lstParams.Add(new SqlParameter("@PageIndex", paging.PageIndex));
                lstParams.Add(new SqlParameter("@PageSize", paging.PageSize));
                lstParams.Add(new SqlParameter("@Sorting", paging.Sorting));
                lstParams.Add(new SqlParameter("@isAllBranch", IFRS_ALLBRANCH));
                lstParams.Add(new SqlParameter("@sCostCenter", sIFRS_COST_CENTER));

                lstParams.Add(new SqlParameter("@TransactionDate", individualImpair.TransactionDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@SourceDataID", individualImpair.SourceDataID.AsDBValue()));
                lstParams.Add(new SqlParameter("@AccountNo", individualImpair.AccountNo.AsDBValue()));
                lstParams.Add(new SqlParameter("@PaymentDate", individualImpair.PaymentDate.AsDBValue()));
                dt = Utility.StoredProcDataTable("SPR_INDIVIDUALIMPAIR_SEARCHALL", lstParams.ToArray());

                iRecordCount = (int)lstParams[0].Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dt;
        }

        public DataTable SEARCH_ALLWIN25(IndividualImpair individualImpair, Paging paging, bool IFRS_ALLBRANCH, string sIFRS_COST_CENTER, ref int iRecordCount)
        {
            DataTable dt = new DataTable();
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@RecordCount", SqlDbType.Int));
                lstParams[0].Direction = ParameterDirection.Output;
                lstParams.Add(new SqlParameter("@PageIndex", paging.PageIndex));
                lstParams.Add(new SqlParameter("@PageSize", paging.PageSize));
                lstParams.Add(new SqlParameter("@Sorting", paging.Sorting));
                lstParams.Add(new SqlParameter("@isAllBranch", IFRS_ALLBRANCH));
                lstParams.Add(new SqlParameter("@sCostCenter", sIFRS_COST_CENTER));

                lstParams.Add(new SqlParameter("@SRC_SYSTEM_ID", individualImpair.SRC_SYSTEM_ID.AsDBValue()));
                lstParams.Add(new SqlParameter("@SUB_ACCNO", individualImpair.SUB_ACCNO.AsDBValue()));
                lstParams.Add(new SqlParameter("@IND_IMPAIR_FG", individualImpair.IND_IMPAIR_FG.AsDBValue()));
                lstParams.Add(new SqlParameter("@CASH_FLOW_FG", individualImpair.CASH_FLOW_FG.AsDBValue()));
                dt = Utility.StoredProcDataTable("SPR_VWINDIVIDUALFILTER_SEARCHALL", lstParams.ToArray());

                iRecordCount = (int)lstParams[0].Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dt;
        }

        public DataTable SEARCH_BYID(int IndividualImpairID)
        {
            DataTable dt = new DataTable();
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@IndividualImpairID", IndividualImpairID));
                dt = Utility.StoredProcDataTable("SPR_INDIVIDUALIMPAIR_SEARCHBYID", lstParams.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dt;
        }

        public int INSERT(IndividualImpair individualImpair)
        {
            int iNum = 0;
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();

            try
            {
                lstParams.Add(new SqlParameter("@IndividualImpairID", SqlDbType.Int));
                lstParams[0].Direction = ParameterDirection.Output;
                lstParams.Add(new SqlParameter("@TransactionDate", individualImpair.TransactionDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@SourceDataID", individualImpair.SourceDataID.AsDBValue()));
                lstParams.Add(new SqlParameter("@AccountNo", individualImpair.AccountNo.AsDBValue()));
                lstParams.Add(new SqlParameter("@PaymentDate", individualImpair.PaymentDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@IndividualImpairAmount", individualImpair.IndividualImpairAmount.AsDBValue()));
                lstParams.Add(new SqlParameter("@CollateralAmount", individualImpair.CollateralAmount.AsDBValue()));
                lstParams.Add(new SqlParameter("@InterestRate", individualImpair.InterestRate.AsDBValue()));
                lstParams.Add(new SqlParameter("@IsConfirmed", individualImpair.IsConfirmed.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedDate", individualImpair.CreatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedBy", individualImpair.CreatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedCostCenter", individualImpair.CreatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedBranch", individualImpair.CreatedBranch.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedDate", individualImpair.UpdatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBy", individualImpair.UpdatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedCostCenter", individualImpair.UpdatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBranch", individualImpair.UpdatedBranch.AsDBValue()));

                Utility.ExecuteStoredProc("SPR_INDIVIDUALIMPAIR_INSERT", lstParams.ToArray());

                iNum = (int)lstParams[0].Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }

        public int UPDATE(IndividualImpair individualImpair)
        {
            int iNum = 0;
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@IndividualImpairID", individualImpair.IndividualImpairID.AsDBValue()));
                lstParams.Add(new SqlParameter("@TransactionDate", individualImpair.TransactionDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@SourceDataID", individualImpair.SourceDataID.AsDBValue()));
                lstParams.Add(new SqlParameter("@AccountNo", individualImpair.AccountNo.AsDBValue()));
                lstParams.Add(new SqlParameter("@PaymentDate", individualImpair.PaymentDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@IndividualImpairAmount", individualImpair.IndividualImpairAmount.AsDBValue()));
                lstParams.Add(new SqlParameter("@CollateralAmount", individualImpair.CollateralAmount.AsDBValue()));
                lstParams.Add(new SqlParameter("@InterestRate", individualImpair.InterestRate.AsDBValue()));
                lstParams.Add(new SqlParameter("@IsConfirmed", individualImpair.IsConfirmed.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedDate", individualImpair.UpdatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBy", individualImpair.UpdatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedCostCenter", individualImpair.UpdatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBranch", individualImpair.UpdatedBranch.AsDBValue()));

                iNum = (int)Utility.ExecuteStoredProc("SPR_INDIVIDUALIMPAIR_UPDATE", lstParams.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }

        public int DELETE(IndividualImpair individualImpair)
        {
            int iNum = 0;
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@IndividualImpairID", individualImpair.IndividualImpairID.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedDate", individualImpair.UpdatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBy", individualImpair.UpdatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedCostCenter", individualImpair.UpdatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBranch", individualImpair.UpdatedBranch.AsDBValue()));
                iNum = (int)Utility.ExecuteStoredProc("SPR_INDIVIDUALIMPAIR_DELETE", lstParams.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }
    }
}